<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * HITI AdminCP
 *
 * @package     HITI AdminCP Project
 * @since       Monday, October 21, 2013, 6:00 PM
 * @final       
 * @category    Models
 * @see 		Contacts
 * @author      PHUOCNGUYEN - HITI Dev Team
 * @copyright   Copyright (c) 2013, HITI Corp
 */

class Contacts_model extends CI_Model {
	public function __construct()
	{
		parent::__construct();
	}

	public function show($uri, $num_rows = false)
	{
		$this->db->query('SET NAMES "LATIN1"');
		if ($uri['limit'] == '') $uri['limit'] = 50;
		if ($uri['q'] != '')
		{
			$this->db->like('name_vn', $uri['q']);
			$this->db->or_like('title', $uri['q']);
		}
		if ($uri['from'] != '')
		{
			$this->db->where(sprintf('date_added >= %1$s', $this->db->escape($uri['from'].' 00:00:00')));
		}
		if ($uri['to'] != '')
		{
			$this->db->where(sprintf('date_added <= %1$s', $this->db->escape($uri['to'].' 23:59:59')));
		}
		$this->db->where('deleted', $uri['deleted']);
		if ($num_rows)
		{
			$this->db->select('id');
			return $this->db->get('contacts')->num_rows();
		}
		else
		{
			$last_login = $GLOBALS['user']['last_login'];
			$this->db->select('*, IF ('.sprintf('date_added >= %1$s', $this->db->escape($last_login)).', 1, 0) as isnew', false);
			$this->db->limit($uri['limit'], $uri['rowstart']);
			if ($uri['orderby'] == '') $uri['orderby'] = 'id';
			if ($uri['ordermode'] == '') $uri['ordermode'] = 'desc';
			$this->db->order_by($uri['orderby'].' '.$uri['ordermode']);
			$query = $this->db->get('contacts');
			if ($query->num_rows() > 0)
			{
				return $query->result_array();
			}
			else
			{
				return false;
			}
		}
	}

	public function info($id)
	{
		$this->db->query('SET NAMES "LATIN1"');
		$this->db->where('id', $id);
		$query = $this->db->get('contacts');
		if ($query->num_rows() > 0)
		{
			return $query->row_array();
		}
		else
		{
			$fields = $this->db->field_data('contacts');
			$default_data = array();
			foreach ($fields as $field)
			{
				$default_data[$field->name] = $field->default;
			}
			return $default_data;
		}
	}

	public function process($data, $id = '')
	{
		if (!is_array($data) || !$id)
		{
			return false;
		}
		$this->db->query('SET NAMES "LATIN1"');
		$title = $data['title'];
		unset($data['title']);
		$this->db->where('id', $id);
		$this->db->update('contacts', $data);
		write_log('update_contacts', $title);
		return true;
	}
}

/* End of file contacts_model.php */
/* Location: ./application/models/contacts_model.php */